<template>
  <view>
    <!-- 搜索 -->
    <my-search bgcolor="#ff00ff" radius="20" @click="gotoSearch"></my-search>
    
    <!-- 分类导航 -->
    <view class="cate-box">
      <!-- 左侧一级导航 -->
      <scroll-view class="scroll-left" scroll-y="true" :style="{height: wh + 'px'}">
        <view
          :class="['scroll-left-item', i===active? 'active': '']"
          v-for="(item, i) in cateList"
          :key="i"
          @click="changeActive(i)"
        >
          {{item.cat_name}}
        </view>
      </scroll-view>
      
      
      <!-- 右侧二级导航 -->
      <scroll-view class="scroll-right" scroll-y="true" :scroll-top="scrollTop" :style="{height: wh + 'px'}" >
        <view class="scroll-right-item" v-for="(item, i) in cateLve2" :key="i">
          <!-- 二级分类title -->
          <view class="right-item-title">
            /{{item.cat_name}}/
          </view>
          <!-- 二级分类详情 -->
          <view class="right-item-detail">
            <view class="detail-item" v-for="(item2, i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
              <image :src="item2.cat_icon" mode="widthFix"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
      
    </view>
  </view>
</template>

<script>
  // import setTabBar from '@/mixin/setTabBar.js'
  
  export default {
    // mixins: [setTabBar], 
    data() {
      return {
        // 页面UI状态
        wh: 0, // 页面状态
        active: 0, // 选中的一级分类index
        scrollTop: 10, // scrollbar 举例顶部的位置
        
        // 页面数据
        cateList: [], // 分类总数据
        cateLve2: [], // 二级分类数据
      };
    },
    onLoad() {
      // 动态获取屏幕可用高度
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight -50 
      
      // 获取分类数据
      this.getCateList()
    },
    methods: {
      // 获取分类数据
      async getCateList () {
        const {data: res} = await uni.$http.get('/categories')
        console.log(res)
        // 异常判断
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 设置页面数据
        this.cateList = res.message
        // 二级分类数据
        this.cateLve2 = res.message[0].children
      },
      // 一级分类点击事件处理函数
      changeActive(i){
        // 改变一级分类索引
        this.active = i
        // 修改二级分类数据
        this.cateLve2 = this.cateList[i].children
        // 动态设置scroll bar 到顶部的距离
        this.scrollTop = Math.random()
      },
      // 二级分类点击事件处理函数
      gotoGoodsList(goods){
        // console.log(goods)
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cid=${goods.cat_id}`
        })
      },
      // 搜索组件事件监听函数
      gotoSearch(e){
        console.log('============', e)
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
      
    }
  }
</script>

<style lang="scss">
  .cate-box {
    display: flex;
    
    .scroll-left {
      height: 500rpx;
      width: 280rpx;
      
      .scroll-left-item {
        height: 120rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 26rpx;
        &.active {
          background-color: white;
          display: flex;
          justify-content: space-between;
          &::before {
            content: '';
            width: 4rpx;
            height: 60rpx;
            background-color: #c00000;
          }
          &::after {
            content: '';
            width: 4rpx;
            height: 60rpx;
            background-color: white;
          }
        }
      }
    }
    
    .scroll-right {
      background-color: white;
      .scroll-right-item {
        
        .right-item-title {
          height: 80rpx;
          display: flex;
          justify-content: center;
          align-items: center;
          font-weight: bold;
        }
        
        .right-item-detail {
          display: flex;
          flex-wrap: wrap;
          
          .detail-item {
            width: 33.33%;
            padding: 8rpx;
            box-sizing: border-box;
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            image {
              width: 100%;
            }
            text {
              font-size: 24rpx;
            }
            
          }
        }
        
      }
    }
  }
</style>
